<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用组件实现购物车案例</title>
    <!-- 导入vue库依赖 -->
    <script src="./js/vue.js"></script>
    <script src="./js/vue-router.js"></script>
    <style>
        /* 样式拷贝 */
        
        .container .cart {
            width: 300px;
            margin: auto;
        }
        
        .container .title {
            background-color: lightblue;
            height: 40px;
            line-height: 40px;
            text-align: center;
            /*color: #fff;*/
        }
        
        .container .total {
            background-color: #FFCE46;
            height: 50px;
            line-height: 50px;
            text-align: right;
        }
        
        .container .total button {
            margin: 0 10px;
            background-color: #DC4C40;
            height: 35px;
            width: 80px;
            border: 0;
        }
        
        .container .total span {
            color: red;
            font-weight: bold;
        }
        
        .container .item {
            height: 55px;
            line-height: 55px;
            position: relative;
            border-top: 1px solid #ADD8E6;
        }
        
        .container .item img {
            width: 45px;
            height: 45px;
            margin: 5px;
        }
        
        .container .item .name {
            position: absolute;
            width: 90px;
            top: 0;
            left: 55px;
            font-size: 16px;
        }
        
        .container .item .change {
            width: 100px;
            position: absolute;
            top: 0;
            right: 50px;
        }
        
        .container .item .change a {
            font-size: 20px;
            width: 30px;
            text-decoration: none;
            background-color: lightgray;
            vertical-align: middle;
        }
        
        .container .item .change .num {
            width: 40px;
            height: 25px;
        }
        
        .container .item .del {
            position: absolute;
            top: 0;
            right: 0px;
            width: 40px;
            text-align: center;
            font-size: 40px;
            cursor: pointer;
            color: red;
        }
        
        .container .item .del:hover {
            background-color: orange;
        }
    </style>
</head>

<body>

    <div id="app" class="container">
        <!--组件化构建布局 -->
        <layout-base></layout-base>
    </div>


    <script>
        // 组件化标题组件
        var Header = {
            props: ['title'],
            data: function() {
                return {}
            },
            template: `<div class="title">{{title}}的商品</div>`
        }

        // 组件化中间布局组件
        var Main = {
            props: ['data'],
            data: function() {
                return {}
            },
            template: `
            <div>
                <div class="item" :key= 'item.id'v-for='item in data'>
                <img :src="item.img" />
                <div class="name">{{item.name}}</div>
                <div class="change">
                    <a href="" @click.prevent='sub(item.id)'>－</a>
                    <input type="text" class="num" :value='item.num' @blur='change($event, item.id)'/>
                    <a href="" @click.prevent='add(item.id)'>＋</a>
                </div>
                <div class="del" @click='del(item.id)'>×</div>
                </div>
            </div>
            `,
            methods: {
                // 设置焦点失去事件 获取输入的个数和id的值
                change: function(event, id) {
                    console.log(event.target.value, id);
                    var obj = {
                        id: id,
                        flag: 'change',
                        num: event.target.value
                    }
                    this.$emit('input-change', obj)
                },
                // 删除该条数据
                del: function(id) {
                    console.log('删除该条数据 id值 ' + id);
                    this.$emit('del', id)
                },
                sub: function(id) {
                    var info = {
                        id: id,
                        flag: 'sub'
                    }
                    this.$emit('input-change', info)
                },
                add: function(id) {
                    var info = {
                        id: id,
                        flag: 'add'
                    }
                    this.$emit('input-change', info)
                }
            }
        }

        // 组件化底部组件
        var Footer = {
            props: ['data'],
            data: function() {
                return {

                }
            },
            template: ` 
            <div class="total">
                <span>总价:{{total}}</span>
                <button>结算</button>
            </div>`,
            // 设置计算属性
            computed: {
                total: function() {
                    var sum = 0
                    this.data.forEach(element => {
                        sum += element.num * element.price
                    })
                    return sum
                }
            }
        }

        // 设置全局组件挂载

        Vue.component('layout-base', {
            data: function() {
                return {
                    name: '杜文俊',
                    data: [{
                        id: 1,
                        name: 'TCL彩电',
                        price: 1000,
                        num: 1,
                        img: 'img/a.jpg'
                    }, {
                        id: 2,
                        name: '机顶盒',
                        price: 1000,
                        num: 1,
                        img: 'img/b.jpg'
                    }, {
                        id: 3,
                        name: '海尔冰箱',
                        price: 1000,
                        num: 1,
                        img: 'img/c.jpg'
                    }, {
                        id: 4,
                        name: '小米手机',
                        price: 1000,
                        num: 1,
                        img: 'img/d.jpg'
                    }, {
                        id: 5,
                        name: 'PPTV电视',
                        price: 1000,
                        num: 2,
                        img: 'img/e.jpg'
                    }]
                }
            },
            template: `
            <div class="cart">
            <my-header :title='name'></my-header>
            <my-main :data='data' @input-change='change($event)' @del='del($event)'></my-main>
            <my-footer :data='data'></my-footer>
            </div>
            `,
            components: {
                'my-header': Header,
                'my-main': Main,
                'my-footer': Footer
            },
            methods: {
                change: function(info) {
                    console.log(info);
                    // 检查数组中是否有符合条件的对象

                    if (info.flag === 'change') {
                        this.data.some(function(item) {
                            if (item.id === info.id) {
                                item.num = info.num
                                return true
                            }
                        })
                    } else if (info.flag === 'sub') {
                        this.data.some(function(item) {
                            if (item.id === info.id) {
                                // 当数量低于0时 不能再往下减少了
                                if (item.num > 0) {
                                    item.num -= 1
                                    return true
                                }
                            }
                        })
                    } else {
                        this.data.some(function(item) {
                            if (item.id === info.id) {
                                item.num += 1
                                return true
                            }
                        })

                    }

                },
                del: function(id) {
                    console.log('父组件中要删除的是' + id);
                    var index = this.data.findIndex(function(item) {
                        if (item.id === id) {
                            return true
                        }
                    })
                    this.data.splice(index, 1)
                }
            }
        })
        var vm = new Vue({
            el: '#app',
            data: {
                msg: ''
            },
            methods: {},
        })
    </script>
</body>

</html>